﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using InternshipManagement.Models;
using InternshipManagement.Helpers;

namespace InternshipManagement.Controllers
{
    [CustomAuthorize]
    public class CalendarEventController : Controller
    {
        CalendarEventRepository calendarEventRepository = new CalendarEventRepository();
        CourseRepository courseRepository = new CourseRepository();
        AnnouncementRepository announcementRepository = new AnnouncementRepository();
        AccountRepository accountRepository = new AccountRepository();
        //
        // GET: /CalendarEvent/

        [CustomAuthorize(Privileges = UserPrivileges.CalendarManagement)]
        public ActionResult Index()
        {
            List<Course> courses = courseRepository.FindAllActiveCourses().ToList();
            ViewBag.Courses = courses;
            if (courses.Count == 0)
                ViewBag.Students = new List<Student>();
            else
                ViewBag.Students = accountRepository.FindAllStudents((int)ViewBag.Courses[0].CourseID).Select(s => new { s.ID, DisplayName = s.Name + " " + s.Surname }).ToList();
            return View(calendarEventRepository.FindAllCalendarEvents().ToList());
        }

        [CustomAuthorize(Privileges = UserPrivileges.CalendarManagement)]
        public JsonResult CreateEditEvent(CalendarEvent calendarEvent)
        {
            if (ModelState.IsValid || calendarEvent.StartDate > calendarEvent.EndDate)
            {
                if (calendarEvent.EventID == 0)
                {
                    Announcement announcement = new Announcement();
                    announcement.Title = calendarEvent.Title;
                    announcement.Message = "Last week before " + announcement.Title;
                    if (calendarEvent.UserID != null)
                    {
                        announcement.IsStudentReceiving = true;
                    }
                    announcement.SenderID = (int)Session["UserID"];
                    announcement.Status = "A";
                    announcement.SentDate = calendarEvent.EndDate.Value.AddDays(-7);
                    announcement.CourseID = calendarEvent.CourseID;
                    announcement.ReceiverID = calendarEvent.UserID;
                    announcementRepository.AddAnnouncement(announcement);
                    announcementRepository.Save();
                    calendarEvent.AnnouncementID = announcement.ID;
                    calendarEventRepository.AddCalendarEvent(calendarEvent);
                    calendarEventRepository.Save();
                    return Json(new
                    {
                        Result = true,
                        EventID = calendarEvent.EventID,
                        CourseID = calendarEvent.CourseID,
                        Course = calendarEvent.Course.DisplayName,
                        UserID = calendarEvent.UserID,
                        User = calendarEvent.UserID == null ? null : calendarEvent.User.Student.DisplayName,
                        EventType = calendarEvent.EventType,
                        Message = calendarEvent.Message
                    });
                }
                else
                {
                    CalendarEvent cEvent = calendarEventRepository.GetCalendarEvent(calendarEvent.EventID);
                    cEvent.Title = calendarEvent.Title;
                    cEvent.Message = calendarEvent.Message;
                    cEvent.StartDate = calendarEvent.StartDate;
                    cEvent.EndDate = calendarEvent.EndDate;
                    cEvent.TextColor = calendarEvent.TextColor;
                    cEvent.BGColor = calendarEvent.BGColor;
                    cEvent.EventType = calendarEvent.EventType;
                    if (ModelState.IsValid)
                    {
                        Announcement announcement = announcementRepository.GetAnnouncement(cEvent.AnnouncementID.Value);
                        announcement.Title = cEvent.Title;
                        announcement.Message = "Last week before " + announcement.Title;
                        if (cEvent.UserID != null)
                        {
                            announcement.IsStudentReceiving = true;
                        }
                        announcement.SenderID = (int)Session["UserID"];
                        announcement.Status = "A";
                        announcement.SentDate = cEvent.EndDate.Value.AddDays(-7);
                        announcement.CourseID = cEvent.CourseID;
                        announcement.ReceiverID = cEvent.UserID;
                        announcementRepository.Save();
                        calendarEventRepository.Save();
                        return Json(new
                        {
                            Result = true,
                            EventID = cEvent.EventID,
                            CourseID = cEvent.CourseID,
                            Course = cEvent.Course.DisplayName,
                            UserID = cEvent.UserID,
                            User = cEvent.UserID == null ? null : cEvent.User.Student.DisplayName,
                            EventType = cEvent.EventType,
                            Message = calendarEvent.Message
                        });
                    }
                }
            }

            string errorMessage = "<div class=\"validation-summary-errors\">"
                            + "The following errors occurred:<ul>";
            foreach (var key in ModelState.Keys)
            {
                var error = ModelState[key].Errors.FirstOrDefault();
                if (error != null)
                {
                    errorMessage += "<li class=\"field-validation-error\">"
                     + error.ErrorMessage + "</li>";
                }
            }
            return Json(new { Result = false, ErrorMessage = errorMessage });

        }

        [CustomAuthorize(Privileges = UserPrivileges.CalendarManagement)]
        public JsonResult DeleteEvent(int id)
        {
            CalendarEvent calendarEvent = calendarEventRepository.GetCalendarEvent(id);
            calendarEventRepository.DeleteCalendarEvent(calendarEvent);
            calendarEventRepository.Save();
            return Json(new { Result = true });
        }

        [CustomAuthorize]
        public ActionResult MiniCalendar()
        {
            return View();
        }
        [CustomAuthorize]
        public JsonResult GetEvents()
        {
            return Json(calendarEventRepository.FindAllCalendarEvents((int)Session["UserID"]).Select(e => new
            {
                e.Title,
                e.StartDate,
                e.Message,
                e.UserID,
                User = e.UserID == null ? "" : e.User.Student.Name + " " + e.User.Student.Surname,
                Course = e.CourseID == null ? "" : e.Course.Code + "-" + e.Course.Semester.Season
            }).ToList(), JsonRequestBehavior.AllowGet);
        }



    }
}
